package com.lwj.widget.viewpagerindicator_demo.slice;

import com.lwj.widget.viewpagerindicator.ResourceTable;
import com.lwj.widget.viewpagerindicator.ViewPagerIndicator;
import com.ms.banner.Banner;
import com.ms.banner.Utils;
import com.ms.banner.listener.OnBannerClickListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * BannerViewAbilitySlice
 *
 * @author name
 * @since 2021-03-20
 */
public class BannerViewAbilitySlice extends AbilitySlice {
    private static final int DELAYTIME = 2000;
    private static final int TEXTSIZE = 40;
    private static final int DURATIONTIME = 3000;
    private static ViewPagerIndicator notCircleIndicator;
    private static ViewPagerIndicator notCircleLineIndicator;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_banner_view);
        initListContainer();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initListContainer() {
        Text text = (Text) findComponentById(ResourceTable.Id_banner_text);
        notCircleIndicator = (ViewPagerIndicator) findComponentById(ResourceTable.Id_not_circle_indicator);
        notCircleLineIndicator = (ViewPagerIndicator) findComponentById(ResourceTable.Id_not_circle_line_indicator_ban);
        Banner banner = (Banner) findComponentById(ResourceTable.Id_banner);
        if (banner == null) {
            return;
        }
        banner.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            (int) (Utils.getInstance().getScreenPiex(this).height)));
        ArrayList<Integer> mImageIds = getDataInt();

        // 简单使用
        banner.setAutoPlay(true)
            .setPages(mImageIds, new CustomViewHolder())
            .setDelayTime(DELAYTIME)
            .setOnPageChangeListener(new PageSlider.PageChangedListener() {
                @Override
                public void onPageSliding(int m, float v, int n) {

                }
                @Override
                public void onPageSlideStateChanged(int i) {
                    if (i == 0) {
                        text.setText(mImageIds.get(banner.getSlider().getCurrentPage()));
                        text.setMultipleLine(true);
                    }
                }
                @Override
                public void onPageChosen(int i) {

                }
            })
            .setOnBannerClickListener(new OnBannerClickListener() {
                @Override
                public void onBannerClick(List datas, int position) {
                    Text dialogText = new Text(banner.getContext());
                    dialogText.setMultipleLine(true);
                    dialogText.setText(getDataInt().get(position));
                    dialogText.setTextSize(TEXTSIZE);
                    new ToastDialog(banner.getContext())
                        .setComponent(dialogText)
                        .setDuration(DURATIONTIME)
                        // Toast显示在界面中间
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
                }
            })
            .start();
        notCircleIndicator.setViewPager(banner.getSlider(),getDataInt().size());
        notCircleLineIndicator.setViewPager(banner.getSlider(),getDataInt().size());
    }

    /**
     * 获取网络图片
     *
     * @return 网络图片地址
     */
    private ArrayList<String> getDataStr() {
        ArrayList<String> list = new ArrayList<>();
        list.add("网络图片地址");
        return list;
    }

    /**
     * 获取图片列表
     *
     * @return 本地图片
     */
    public static ArrayList<Integer> getDataInt() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(ResourceTable.Media_m1);
        list.add(ResourceTable.Media_m2);
        list.add(ResourceTable.Media_m3);
        list.add(ResourceTable.Media_m4);
        return list;
    }
}
